package com.kfm.http;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.kfm.commons.Constant;
import com.kfm.config.ChatConfig;
import com.kfm.entity.Message;
import com.kfm.entity.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;

@Component
public class HttpExample {

    @Resource
    private ChatConfig config;

    public String send(String msg) {
        ArrayList<Message> messages = new ArrayList<>();
        messages.add(new Message("user", msg));
        Question question = new Question(config.getModel(), messages);
        String jsonStr = JSONUtil.toJsonStr(question);
        String body = HttpRequest.post(Constant.URL)
                .header(Header.CONTENT_TYPE, "application/json")
                .header(Header.AUTHORIZATION, "Bearer " + config.getApiKey())
                .body(jsonStr)
                .execute().body();
        System.out.println(body);
        return body;
    }
}
